/* eslint-disable no-console */ import Inject from "@entity-access/entity-access/dist/di/di.js"; import TempFileService from "../../../../../../server/storage/TempFileService.js"; import { StatusResult, TempFileResult } from "@entity-access/server-pages/dist/Content.js"; import Page from "@entity-access/server-pages/dist/Page.js"; import { Prepare } from "@entity-access/server-pages/dist/decorators/Prepare.js"; import { join } from "path"; import { tokenizeRegex } from "../../../../../../protocols/core/tokenizer.js"; import { Route } from "@entity-access/server-pages/dist/core/Route.js"; import SessionSecurity from "@entity-access/server-pages/dist/services/SessionSecurity.js"; import PreviewDraftService from "../../../../../../server/services/html/PreviewDraftService.js"; import WebSiteContentService from "../../../../../../server/services/files/WebSiteContentService.js"; import ServerLogger from "@entity-access/server-pages/dist/core/ServerLogger.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; @Prepare.authorize export default class extends Page { @Inject websiteService: WebSiteContentService; @Inject tfs: TempFileService; @Route eFileID: string; @Inject sessionSecurity: SessionSecurity; @Inject previewDraftService: PreviewDraftService; async run() { const path = this.childPath as any[]; const siteID = this.eFileID; if (!/^e(s|p)\-/.test(siteID)) { ServerLogger.error(`Not found ${this.request.url} requested by ${this.request.headers.referer}`); return this.notFound(); } const keys = this.sessionSecurity.decryptKey(siteID); // draft is only visible to logged in user const site = await this.websiteService.getWebSiteFolder(keys.appFileID); if(path.length === 0) { path.push("index.html"); } const _v = site.lastModified ? DateTime.from(site.lastModified).msSinceEpoch.toString(36) : site.folderID.toString(); const file = await this.websiteService.getDbFileInfo(_v, site.folderID, path); if (!file) { console.warn(`404: ${this.request?.URL?.toString()}`); return this.notFound(true); } // we can safely redirect for images // to save bandwidth if (/^(image|video|application)\//i.test(file.contentType)) { const url = await this.websiteService.toEditorDraftCDNUrl({ folderID: site.folderID }, file); return this.redirect(url); } const isCss = /text\/css/i.test(file.contentType); if (isCss) { // it is safe to assume that CSS will not require any changes // unless etag has been changed // inline resources will be resolved separately via download url let etag = this.request.headers["if-none-match"]; if (/^(\"|\')/i.test(etag)) { etag = JSON.parse(etag); } if (etag === file.fileContentID.toString()) { return new StatusResult(304, { etag: JSON.stringify(file.fileContentID.toString()), "cache-control": "max-age=0, must-revalidate" }); } } const localFile = await this.tfs.downloadFileContent(file.fileContentID, file); // we need to change html if it contains inject if (/text\/html/i.test(file.contentType)) { // we cannot use etag for these requests // as injected content might have changed const body = await this.previewDraftService.prepareSecureHtml(siteID, site, localFile); return this.content({ body, contentType: "text/html"}); } if (isCss) { const all = [ ... path]; all.pop(); const pagePath = join(`/social-mail/site/draft/${siteID}/`, ... all); let body = await localFile.readAsText(); body = updateCss(body, pagePath); return this.content({ body, contentType: "text/css", headers: { etag: JSON.stringify(file.fileContentID.toString()), "cache-control": "max-age=0, must-revalidate" } }); } return new TempFileResult( localFile, { contentDisposition: "inline", etag: false, maxAge: 0 } ); } } const updateCss = (css: string, pagePath: string ) => { for( const [_match, group] of tokenizeRegex(css, /url\(((\"[^\"]+\")|([^\)]+))\)/gmi)) { if (group) { let link = group; const first = link[0]; const encoded = first === '"' || first === "'"; if (encoded) { link = link.substring(1, link.length - 1); } if (link.startsWith(".")) { link = `"${join(pagePath, link)}"`; css = css.replaceAll(group, link); } } } return css; };